﻿//Copyright 2008 Kristian Gundry And Rune Juhl-Petersen
//This file is part of Plain.

//Plain is free software: you can redistribute it and/or modify
//it under the terms of the GNU Lesser General Public License as published by
//the Free Software Foundation, either version 3 of the License, or
//any later version.

//Plain is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//GNU Lesser General Public License for more details.

//You should have received a copy of the GNU Lesser General Public License
//along with Plain.  If not, see <http://www.gnu.org/licenses/>.
using System;
using System.Collections.Generic;
using System.Text;
using Plain.Validation;
using System.Text.RegularExpressions;
using System.Reflection;

namespace Plain.Mapping
{
    public class ValidationMapping {
        public static string GetMessage(IValidationAttribute att) {
            ValidationMapping map = new ValidationMapping();
            string message = "this is a test {=Min}, {=Max} ";
            return map.parseMessage(message,att);
        }

        public static string GetMessage(IValidationAttribute att,PropertyInfo prop)
        {
            ValidationMapping map = new ValidationMapping();
            ValidationMappingHandler handler = (ValidationMappingHandler)System.Configuration.ConfigurationManager.GetSection("Validations");
            string message = "";
            if (handler != null) {
                message =handler.GetMessage(att.GetType().Name);
            } 
            if(string.IsNullOrEmpty(message))
            {
                message = "Validation failed: " + prop.Name + " failed the " + att.GetType().Name + " validation";
            }                                   
            return map.parseMessage(message, att);
        }

        /// <summary>
        /// Parses a message and adds the content from the properties specified
        /// </summary>
        /// <param name="message"></param>
        /// <param name="o"></param>
        /// <example>
        /// string message = "This is an error message. Please provide a number greater than {=Min}";
        /// Range range = new Range();
        /// range.Min = 4;
        /// Console.WriteLine(parseMessage(message,range));
        /// 
        /// //prints out: This is an error message. Please provide a number greater than 4
        /// </example>
        /// <returns></returns>
        private string parseMessage(string message, object o)
        {
            //find all parts resembling {=PropertyName}
            Regex reg = new Regex(@"\{\=?([\w])+\}");
            foreach (Match m in reg.Matches(message))
            {
                string v = m.Value;
                try
                {
                    //Extract propertyname
                    v = v.Substring(2, v.Length - 3);
                    if (!string.IsNullOrEmpty(v))
                    {
                        //get property
                        PropertyInfo prop = o.GetType().GetProperty(v);
                        //Create regex that 
                        Regex subReg = new Regex(@"\{\=" + v + @"\}");
                        //Replace with new value
                        message = subReg.Replace(message, prop.GetValue(o, null).ToString());
                    }
                }
                catch
                { //do nothing, if error value is not inserted
                }
            }
            return message;
        }
    }
}
